package com.begonzales.locker.dto;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: begonza
 * Date: 9/19/11
 * Time: 10:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class HeaderItemDTO {

    private String headerName = "";
    private String itemName = "";

    private HeaderDTO header;
    private List<ItemDTO> item;


    public HeaderItemDTO()
    {
        header = new HeaderDTO();
        item = new ArrayList<ItemDTO>();
    }


    public void toFile(String fileName)
    {
        try {
            File removeFile = new File(fileName);
            if ( removeFile.exists())
            {
                removeFile.delete();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            out.write( this.toString() );
            out.close();
        } catch (IOException e) {
        }
    }

    public String toString()
    {
        String txt = "" +
                "package com.nike.brand.thelocker.dto.transaction; \r\n\r\n" +
                "import com.nike.brand.thelocker.domain.ISOLanguageCode; \r\n" +
                "import javax.xml.bind.annotation.*;  \r\n" +
                "import javax.util.Calendar;  \r\n\r\n" +
                this.getPropOrder() +
                this.getHeader().annotationToString() +
                "public class " + this.getHeaderName()  + "\r\n" +
                "{ \r\n" +
                this.getItemDeclarationString() +
                "\r\n" +
                this.getSetPropertyString() +
                this.getGetPropertyString() +
                "} \r\n" ;
        return txt;
    }

    public String getPropOrder()
    {
         String retval = "";
         int ctr = 1;
         retval = retval + "" +
                 "@XmlType(name = \"\", propOrder = {   \r\n";
         for(ItemDTO itemDecl : item)
         {
            if( 1 == ctr )
            {
                retval = retval + "\t" + itemDecl.getPropOrder() + "\r\n";
            }
            else
            {
                retval = retval + "\t," +  itemDecl.getPropOrder() + "\r\n";
            }
            ctr++;
         }
        retval = retval + "})  \r\r";
        return retval;
    }

    public String getItemDeclarationString()
    {
        String retval = "";
         for(ItemDTO itemDecl : item)
         {
             retval = retval + itemDecl.getVarDeclarationString();
         }
        return retval;
    }

    public String getSetPropertyString()
    {
        String retval = "";
         for(ItemDTO itemDecl : item)
         {
             retval = retval + itemDecl.getSetPropertyString();
         }
        return retval;
    }
    public String getGetPropertyString()
    {
        String retval = "";
         for(ItemDTO itemDecl : item)
         {
             retval = retval + itemDecl.getGetPropertyString();
         }
        return retval;
    }

    public List<ItemDTO> getItem() {
        return item;
    }

    public ItemDTO addItem(String itemName, String itemJavaType, String itemAccessName)
    {
        ItemDTO itm = new ItemDTO(itemName, itemJavaType, itemAccessName);
        this.getItem().add(itm);
        return itm;
    }
    public ItemDTO add(String itemName, String itemJavaType, String itemAccessName)
    {
        ItemDTO itm = new ItemDTO(itemName, itemJavaType, itemAccessName);
        this.getItem().add(itm);
        return itm;
    }
    public ItemDTO addItem(ItemDTO itemDTO)
    {
        ItemDTO itm = itemDTO;
        this.getItem().add(itm);
        return itm;
    }

    public ItemDTO add(ItemDTO itemDTO)
    {
        ItemDTO itm = itemDTO;
        this.getItem().add(itm);
        return itm;
    }

    public HeaderDTO getHeader() {
        return header;
    }

    public String getHeaderName() {
        return headerName;
    }

    public void setHeaderName(String headerName) {
        this.headerName = headerName;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
}
